﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace bxml.betterxml.handlers
{
    class PrintWriter : IDisposable
    {
        private StreamWriter writer;

        public PrintWriter(StreamWriter sw)
        {
            writer = sw;
        }

        public void Dispose()
        {
            if (writer != null)
            {
                writer.Close();
            }
        }

        public void print(StringBuilder sb)
        {
            printf(sb.ToString());
        }

        public void printf(string text, params object[] paramters)
        {
            string message = string.Format(text, paramters);
            if (writer != null)
            {
                writer.Write(message);
            }
        }

        public void println(string text)
        {
            printf(text);
            printf("\r\n");
        }
    }

    public class XIREchoHandler : BetterXmlContentHandler
    {
        private StringBuilder tabs = new StringBuilder();
        private PrintWriter printer;

        private void tab()
        {
            printer.print(tabs);
        }

        private void indent()
        {
            tabs.Append(' ');
        }

        private void outdent()
        {
            tabs.Remove(tabs.Length - 1, 1);
        }

        public XIREchoHandler(StreamWriter output)
        {
            printer = new PrintWriter(output);
        }

        public void attribute(String nsURI, String name, String qname, String value)
        {
            indent();
            tab();
            printer.printf("attribute %s:%s=%s\n", nsURI, name, value);
            outdent();
        }

        public void characters(int length, String cdata)
        {
            tab();
            printer.printf("characters %s (len=%d)\n", new Object[] { cdata, length });

        }

        public void endDocument()
        {
            outdent();
            tab();
            printer.println("document:end");
        }

        public void endElement(String nsURI, String name, String qname)
        {
            outdent();
            tab();
            printer.printf("element:end %s:%s\n", new Object[] { nsURI, name });
        }

        public void endPrefixMapping(String nsPrefix)
        {
            tab();
            printer.printf("prefix:end %s\n", new Object[] { nsPrefix });
        }

        public void processingInstruction(String name, String target)
        {
            tab();
            printer.printf("pi %s=%s", new Object[] { name, target });
        }

        public void skippedEntity(String name)
        {
            tab();
            printer.printf("skipped-entity %s", new Object[] { name });

        }

        public void startDocument()
        {
            tab();
            printer.println("document:begin");
            indent();
        }

        public void startElement(String nsURI, String name, String qname, int numberOfAttrs)
        {
            tab();
            printer.printf("element:start %s:%s #attrs %d", new Object[] { nsURI, name, numberOfAttrs });
            indent();
        }

        public void startPrefixMapping(String nsPrefix, String nsURI)
        {
            tab();
            printer.printf("prefix:begin %s", new Object[] { nsPrefix, nsURI });

        }

        public void whitespace(int length, String cdata)
        {
            tab();
            printer.printf("characters %s (len=%d)", new Object[] { cdata, length });

        }
    }

}
